<?php

namespace app\test\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    protected $BusinessModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business');
    }


    public function index()
    {
        $keyword = $this->request->param('keyword', '', 'trim');
        $gender = $this->request->param('gender', 0, 'trim');

        $where = [
            'auth' => 0,
            'gender' => $gender
        ];

        if ($keyword) {
            $where['mobile|nickname'] = ['like', "%$keyword%"];
        }

        ## table方法必须指定完整的数据表名
        // $businessData = Db::table('pre_business')->select();

        ## 设置了数据表前缀参数可以使用name方法
        $businessData = Db::name('business')
            // ->where('mobile', 'LIKE', "%$keyword%")
            // ->whereTime('createtime', 'month')
            ->where($where)
            ->order('id desc')
            ->select();


        ## 打印数据方法：var_dump 和 halt
        // var_dump($businessData);
        // exit;

        // halt($businessData);
        // echo 1;


        $this->assign([
            'test' => '客户表',
            'businessData' => $businessData,
            'keyword' => $keyword
        ]);

        ## 返回数据到指定页面中 $this->view->fetch()用于继承模板中，view()直接输出
        return $this->view->fetch();
        // return $this->view->fetch('', ['test' => '客户表', 'businessData' => $businessData]);
        // return view();
    }

    public function info()
    {
        $id = $this->request->param('id', 0, 'trim');

        $info = $this->BusinessModel->where('id', $id)->find($id);


        // $info = Db::name('business')->find($id);
        $info = Db::name('business')->where('id', $id)->find();

        if (!$info) {
            $this->error('用户不存在');
        }

        // halt($info);


        $info['gender_text'] = $this->BusinessModel->getGenderTextAttr($info);
        echo $info['gender_text'];
    }
}
